package com.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.constant.SystemConstants;
import com.common.domain.entity.Menu;
import com.common.domain.entity.ResponseResult;
import com.common.domain.vo.MenuVo;
import com.common.enums.AppHttpCodeEnum;
import com.common.exception.SystemException;
import com.common.service.MenuService;
import com.common.mapper.MenuMapper;
import com.common.util.BeanCopyUtils;
import com.common.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author pc
* @description 针对表【sys_menu(菜单权限表)】的数据库操作Service实现
* @createDate 2023-02-07 12:19:15
*/
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu>
    implements MenuService {
    @Autowired
    private MenuMapper menuMapper;

    @Override
    public List<String> selectPermsByUserId(Long userId) {
        //如果用户id是1，返回所有的权限
        if(userId == 24L){
            LambdaQueryWrapper<Menu> queryWrapper = new LambdaQueryWrapper<>();

            queryWrapper.eq(Menu::getStatus, SystemConstants.STATUS_NORMAL);
            queryWrapper.in(Menu::getMenuType, SystemConstants.MENU, SystemConstants.BUTTON);

            List<Menu> menus = list(queryWrapper);

            List<String> perms = menus.stream()
                    .map(Menu::getPerms)
                    .collect(Collectors.toList());
            return perms;
        }

        return menuMapper.selectPermsByUserId(userId);
    }

    @Override
    public List<MenuVo> selectRouterMenuTreeByUserId(Long userId) {
        List<Menu> menus = null;

        if(SecurityUtils.isAdmin()){
            menus = menuMapper.selectAllRouterMenu();
        }
        else{
            menus = menuMapper.selectAllRouterMenuTreeByUserId(userId);
        }

        List<MenuVo> menuVos = BeanCopyUtils.copyBeanList(menus,MenuVo.class);
        //构建tree节点的第一层  根据parent_id = 0来判断是不是第一层节点

        List<MenuVo> menuTree = builderMenuTree(menuVos,0L);

       return menuTree;
    }

    @Override
    public ResponseResult menuList(String status, String menuName) {
        //封装查询条件
        //	可以针对菜单名进行模糊查询
        //	也可以针对菜单的状态进行查询。
        LambdaQueryWrapper<Menu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.hasText(menuName),Menu::getMenuName, menuName);
        queryWrapper.eq(StringUtils.hasText(status),Menu::getStatus, status);
        queryWrapper.orderByAsc(Menu::getParentId,Menu::getOrderNum);

        //查询
        List<Menu> list = list(queryWrapper);
        List<MenuVo> menuVos = BeanCopyUtils.copyBeanList(list, MenuVo.class);

        return ResponseResult.okResult(menuVos);
    }

    @Override
    public ResponseResult InsertList(Menu menu) {
        save(menu);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getMenu(Long id) {
        Menu byId = getById(id);
        return ResponseResult.okResult(byId);
    }

    @Override
    public ResponseResult updateMenu(Menu menu) {
        //根据id进行查询
        Menu menu1 = getById(menu.getId());
        if(menu1.getParentId().equals(menu.getParentId())){
            throw new SystemException(AppHttpCodeEnum.PARENT_ID_EQUAL);
        }else{
            updateById(menu);
            return ResponseResult.okResult();
        }
    }

    @Override
    public ResponseResult deleteMenu(Long menuId) {
        //根据id进行查询
        List<Menu> list = list();
        //遍历判断是否存在
        long count = list.stream()
                .filter(menu -> menu.getParentId().equals(menuId))
                .count();
        if(count > 0){
            throw new SystemException(AppHttpCodeEnum.CHILDREN_NOT_NULL);
        }else{
            removeById(menuId);
        }
        return ResponseResult.okResult();
    }

    private List<MenuVo> builderMenuTree(List<MenuVo> menuVos, long l) {
        List<MenuVo> collect = menuVos.stream()
                .filter(menuVo -> menuVo.getParentId().equals(l))
                .map(menuVo -> menuVo.setChildren(getChildren(menuVo, menuVos)))
                .collect(Collectors.toList());
        return collect;
    }

    private List<MenuVo> getChildren(MenuVo menuVo, List<MenuVo> menuVos) {
        List<MenuVo> collect = menuVos.stream()
                .filter(m -> m.getParentId().equals(menuVo.getId()))
                .map(m->m.setChildren(getChildren(m,menuVos)))
                .collect(Collectors.toList());
        return collect;
    }
}




